package com.gang.mars.basic.iterable;

import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;

public class GSequenceList<T> implements Iterable<T>
{
    private T[] elementDates;
    private int size;

    public GSequenceList(int capacity)
    {
        this.elementDates = (T[]) new Object[capacity];
        this.size = 0;
    }

    public void add(T t)
    {
        elementDates[size++] = t;
    }

    @Override
    public Iterator<T> iterator() {
        return new GIterator();
    }

    @Override
    public void forEach(Consumer<? super T> action) {
        Iterable.super.forEach(action);
    }

    @Override
    public Spliterator<T> spliterator() {
        return Iterable.super.spliterator();
    }

    private class GIterator implements Iterator
    {
        private int cusor;

        public GIterator() {
            this.cusor = 0;
        }

        @Override
        public boolean hasNext() {
            return cusor < size;
        }

        @Override
        public Object next() {
            return elementDates[cusor++];
        }
    }
}
